package com.example.administrator.im.ui.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.design.widget.TextInputLayout;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.example.administrator.im.R;
import com.example.administrator.im.utils.DialogUtils;
import com.example.administrator.im.utils.EMUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

/**
 * create:2018/10/2 16:15
 * author: 姚鸿杰
 **/
public class LoginActivity extends AppCompatActivity {

    private final String TAG = "LoginActivity";

    private TextInputLayout inputUsername;
    private TextInputLayout inputPassword;

    private EditText et_username;
    private EditText et_password;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            View decorView = getWindow().getDecorView();
            decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            getWindow().setStatusBarColor(Color.TRANSPARENT);
        }

        setContentView(R.layout.activity_login);
        initView();
        EventBus.getDefault().register(this);
    }

    private void initView() {

        inputUsername = (TextInputLayout) findViewById(R.id.inputUsername);
        inputPassword = (TextInputLayout) findViewById(R.id.inputPassword);

        et_username = (EditText) findViewById(R.id.username);
        et_password = (EditText) findViewById(R.id.password);

        et_username.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                inputUsername.setErrorEnabled(false);
            }
        });
        et_password.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                inputPassword.setErrorEnabled(false);
            }
        });

    }

    //登录
    public void login(View v) {

        String username = et_username.getText().toString();
        String password = et_password.getText().toString();

        inputUsername.setErrorEnabled(false);
        inputPassword.setErrorEnabled(false);

        if (TextUtils.isEmpty(username)) {
            inputUsername.setErrorEnabled(true);
            inputUsername.setError("账号不能为空");
            return;
        }
        if (TextUtils.isEmpty(password)) {
            inputPassword.setErrorEnabled(true);
            inputPassword.setError("密码不能为空");
            return;
        }

        DialogUtils.setDialog(this, R.layout.loading);
        DialogUtils.showDialog();

        EMUtils.login(this, username, password);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void LoginFailHint(LoginErrorHint hint) {
        if (hint.code == 202) {   //密码错误
            inputPassword.setErrorEnabled(true);
            inputPassword.setError("密码错误");
        } else if (hint.code == 204) { //用户不存在
            inputUsername.setErrorEnabled(true);
            inputUsername.setError("用户不存在");
        }
    }

    //注册
    public void register(View v) {
        Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
        startActivity(intent);
    }

    //忘记密码
    public void forgetPassword(View v) {

    }

    public static class LoginErrorHint {
        private int code;

        public LoginErrorHint(int code) {
            this.code = code;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
